package jcataloger.beans;

import java.io.File;
import java.io.Serializable;
import java.text.DecimalFormat;

public class ImageItem implements Serializable {
	
	private int fileHash;
	
	private String name;
	
	private boolean isFile;
	
	private String path;
	
	private long fileLength;
	
	private String parentPath;
	
	private int parentHash;
	
	public ImageItem(File item, File parent) {
		super();
		fileHash = item.hashCode();
		name = item.getName();
		isFile = item.isFile();
		path = item.getPath();
		fileLength = item.length();
		if ( parent != null ) {
			parentPath = parent.getPath();
			parentHash = parent.hashCode();
		} else {
			parentPath = "";
			parentHash = 0;
		}
		
	}

	public boolean isUpDirectory() {
		return name != null && name.equals("..");
	}
	
	public boolean isFile() {
		return isFile;
	}

	public String getName() {
		return name;
	}

	public String getPath() {
		return path;
	}
	
	public String getLength() {
		long length = getFileLength();
		if ( length == 0 ) return "";
		long kilos = 0, megas = 0, gigas = 0;
		if ( length/1024 >= 1 ) {
			kilos = length/1024;
		}
		if (kilos/1024 >= 1) {
			megas = kilos/1024;
		}
		if (megas/1024 >= 1) {
			gigas = megas/1024;
		}
		
		String result="";
		
		if (gigas > 0) {
			double d = Double.parseDouble(gigas+"."+megas);
			DecimalFormat formatter = new DecimalFormat("0.0");
			result = formatter.format(d) + " gb";
		}
		else if (megas > 0) {
			double d = Double.parseDouble(megas+"."+kilos);
			DecimalFormat formatter = new DecimalFormat("0.0");
			result = formatter.format(d) + " mb";
		}
		else {
			result = kilos + " kb";
		}
		
		result += " ("+length+" b)";
		return result;
	}
	
	public String getExtension() {
		if ( !isFile() ) return "";
		String name = getName();
		if ( name.indexOf(".") == -1) return "";
		return name.substring(name.indexOf("."));
	}

	public int getFileHash() {
		return fileHash;
	}

	public void setFileHash(int fileHash) {
		this.fileHash = fileHash;
	}

	public long getFileLength() {
		return fileLength;
	}

	public void setFileLength(long fileLength) {
		this.fileLength = fileLength;
	}

	public int getParentHash() {
		return parentHash;
	}

	public void setParentHash(int parentHash) {
		this.parentHash = parentHash;
	}

	public String getParentPath() {
		return parentPath;
	}

	public void setParentPath(String parentPath) {
		this.parentPath = parentPath;
	}

	public void setFile(boolean isFile) {
		this.isFile = isFile;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setPath(String path) {
		this.path = path;
	}

	
	
}
